package me.learningandroid.ch2_6.fragmentdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.fragment.app.Fragment;

public class DetailsFragment extends Fragment {
    @FunctionalInterface
    public interface DeleteCallback {
        void apply(int id);
    }

    @FunctionalInterface
    public interface UpdateCallback {
        void apply(int id, ContactData data);
    }

    // 使用`Fragment.setArguments(bundle)`给`Fragment`传递参数时,bundle中的键
    private static final String ID = "ID";
    private static final String NAME = "NAME";
    private static final String PHONE = "PHONE";

    private int id ;
    private String name ;
    private String phone;
    private DeleteCallback deleteCallback;
    private UpdateCallback updateCallback;

    //  Fragment对象由安卓框架管理，要求必须提供无参数的构建方法。
    // 如果要传递参数，推荐通过`Fragment.setArguments(bundle)`
    public DetailsFragment() {
    }

    // 如果要传递参数，推荐通过`Fragment.setArguments(bundle)`
    public static DetailsFragment newInstance(int id, String name, String phone) {
        DetailsFragment fragment = new DetailsFragment();
        Bundle args = new Bundle();
        args.putInt(ID, id);
        args.putString(NAME, name);
        args.putString(PHONE, phone);
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnDeleteListener(DeleteCallback callback) {
        deleteCallback=callback;
    }

    public void setOnUpdateListener(UpdateCallback callback) {
        updateCallback=callback;
    }

    // `onCreate`
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(getClass().getName(), "onCreate");
        if (getArguments() != null) {
            id = getArguments().getInt(ID);
            name = getArguments().getString(NAME);
            phone = getArguments().getString(PHONE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i(getClass().getName(), "onCreateView");
        View view= inflater.inflate(R.layout.fragment_details, container, false);
        Button saveBtn = view.findViewById(R.id.saveBtn);
        Button resetBtn = view.findViewById(R.id.resetBtn);
        Button deleteBtn = view.findViewById(R.id.deleteBtn);
        EditText editTextName = view.findViewById(R.id.editTextName);
        EditText editTextPhone = view.findViewById(R.id.editTextPhone);

        editTextName.setText(name);
        editTextPhone.setText(phone);

        resetBtn.setOnClickListener((v)-> {
            editTextName.setText(name);
            editTextPhone.setText(phone);
        });
        deleteBtn.setOnClickListener((v)-> {
            if (null!=deleteCallback) {
                deleteCallback.apply(id);
            }
        });
        saveBtn.setOnClickListener((v)-> {
            if (null!=updateCallback) {
                String tmpName=editTextName.getText().toString();
                String tmpPhone=editTextPhone.getText().toString();
                updateCallback.apply(id, new ContactData(tmpName, tmpPhone));
            }
        });

        return view;
    }


}